<template>
  <div class="typemangent">
    <div class="typetop">
      <el-button type="primary" @click="addpagetype">新增页面类型</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%; margin-bottom: 20px">
      <el-table-column prop="id" label="ID"> </el-table-column>
      <el-table-column prop="name" label="类型名称"> </el-table-column>
      <el-table-column prop="createName" label="添加人"> </el-table-column>
      <el-table-column prop="updateTime" label="添加时间"> </el-table-column>
      <el-table-column prop="note" label="备注"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="editType(scope.row)" type="text" size="small">编辑</el-button>
          <el-button @click="deleteType(scope.row.id)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-size="formData.pageSize"
      :current-page="formData.pageNum"
      @current-change="SetPage"
      @size-change="handleSizeChange"
    ></el-pagination>

    <el-dialog :title="title" :visible.sync="dialogFormVisible" center>
      <el-form :model="addformdata" :rules="rules" ref="typeFrom" label-width="80px">
        <el-form-item label="类型名称">
          <el-input v-model="addformdata.name" autocomplete="off" maxlength="15" show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="addformdata.note" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="savePageType">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getTypeList, addAppType, deleteAppType } from "@/api/basisMG";
export default {
  data() {
    return {
      title: "",
      tableData: [],
      dialogFormVisible: false,
      formData: {
        type: 1,
        productType: "",
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      addformdata: {
        name: "",
        note: "",
        productType: "",
        type: 1,
        id: "",
        dataUrl: "",
        pageUrl: "",
      },
      rules: {
        name: [{ required: true, message: "请填写类型名称", trigger: "blur" }],
      },
    };
  },
  methods: {
    // 分页
    SetPage(e) {
      this.formData.pageNum = e;
      this.getTypeList();
    },
    handleSizeChange (val) {
      this.formData.pageSize = val;
      this.getTypeList();
    },
    async getTypeList() {
      const res = await getTypeList(this.formData);
      this.tableData = res.data.content;
      this.total = res.data.total;
    },
    addpagetype() {
      this.title = "新增页面类型";
      this.dialogFormVisible = true;
      this.addformdata.name = "";
      this.addformdata.note = "";
      this.addformdata.id = "";
    },
    savePageType() {
      this.addformdata.name = this.addformdata.name.replace(/[^\a-\z\A-\Z0-9\u4e00-\u9fe5_]/g, "");
      this.addformdata.note = this.addformdata.note.replace(/[^\a-\z\A-\Z0-9\u4e00-\u9fe5_]/g, "");
      this.$refs["typeFrom"].validate(async (valid) => {
        if (valid) {
          const data = await addAppType(this.addformdata);
          if (data.code != 200) return this.$message.error(data.message);
          this.$message.success(data.message);
          setTimeout(() => {
            this.dialogFormVisible = false;
            this.getTypeList();
          }, 500);
        } else {
          this.$message.warning("提交失败, 请检查内容是否填写正确!!!");
          return false;
        }
      });
    },
    editType(row) {
      this.dialogFormVisible = true;
      this.title = "编辑页面类型";
      this.addformdata.name = row.name;
      this.addformdata.note = row.note;
      this.addformdata.id = row.id;
      async () => {
        const data = await addAppType(this.addformdata);
        if (data.code != 200) return this.$message.error(data.message);
        this.$message.success(data.message);
        setTimeout(() => {
          this.dialogFormVisible = false;
          this.getTypeList();
        }, 500);
      };
    },
    deleteType(id) {
      this.$confirm("确定要删除吗？删除后将无法恢复，请谨慎操作", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const data = await deleteAppType({ id: id });
          if (data.code != 200) return this.$message.error("操作失败请稍后重试");
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getTypeList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  created() {
    this.getTypeList();
  },
};
</script>
<style lang="scss">
.typemangent {
  padding: 20px;
  .typetop {
    padding-bottom: 20px;
  }
}
</style>
